package com.heshuo.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import com.heshuo.oc.dao.IAttachDAO;
import com.heshuo.oc.domain.Attach;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;


public class DownloadAction extends ActionSupport
{
	private static final long serialVersionUID = 6329383258366253255L;
	private String guid;
	private String fileName;
	private String fileRealName;
	private IAttachDAO attachDAO;
	
	/*
	 * @getDownloadFile 此方法对应的是struts.xml文件中的： <param
	 * name="inputName">downloadFile</param> 返回下载文件的流，可以参看struts2的源码
	 */
	public InputStream getDownloadFile() throws FileNotFoundException
	{
		
		Attach at=attachDAO.findById(guid);
		setFileName(at.getFileName());
		//创建
		File file = new File(at.getFilePath()); 
		InputStream is = new FileInputStream(file);
		return is;
		//如果文件在项目的上下文环境中，则用下面的输出
		//return ServletActionContext.getServletContext().getResourceAsStream(at.getFilePath());
		
	}

	@Override
	public String execute() throws Exception
	{
		
		
		return SUCCESS;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public String getFileRealName() {
		return fileRealName;
	}

	public void setFileRealName(String fileRealName) {
		this.fileRealName = fileRealName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public IAttachDAO getAttachDAO() {
		return attachDAO;
	}

	public void setAttachDAO(IAttachDAO attachDAO) {
		this.attachDAO = attachDAO;
	}

	public String getFileName() {
		return fileName;
	}
}